package cn.stylefeng.guns.modular.system.service.ActivityEvent;

import cn.stylefeng.guns.modular.system.entity.SalesOrder;
import cn.stylefeng.guns.modular.system.entity.SendGoods;
import cn.stylefeng.guns.modular.system.service.SalesOrderService;
import cn.stylefeng.guns.sys.core.util.utility.StatusCode;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;

public class SaleOrderEvent implements ExecutionListener {
    private SalesOrderService salesOrderService = SpringContextHolder.getBean(SalesOrderService.class);

    @Override
    public void notify(DelegateExecution delegateExecution) {

        String eventName = delegateExecution.getEventName();
        SalesOrder salesOrder = salesOrderService.getOne(new QueryWrapper<SalesOrder>()
                .lambda().eq(SalesOrder::getProcessInstanceId,delegateExecution.getProcessInstanceId()));
        if ("end".equals(eventName)){
            salesOrder.setStatus(StatusCode.FAIL.getVal());
        }else if ("start".equals(eventName)){
            salesOrder.setStatus(StatusCode.SUCCESS.getVal());
            salesOrder.setDealStatus(StatusCode.SENDHAVADOING.getVal());
//            //生成发货单
//            SendGoods sendGoods = new SendGoods();
//            sendGoods.se
        }
        salesOrderService.getBaseMapper().updateById(salesOrder);
    }
}
